﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls;
using Microsoft.Practices.Prism.Mvvm;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Infrastructure;
using Login.Events;
using Login.Views;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.PubSubEvents;
using NetDisk.Service;
using Telerik.Windows.Controls;
using ListBox = System.Windows.Controls.ListBox;

namespace Login.ViewModels
{
    [Export]
    public class RightPaneViewModel: BindableBase
    {
        BitmapImage image;
        private string Mfile = string.Empty;
        private IEventAggregator _eventAggregator;
        private ObservableCollection<Item> dataList;
        public ICommand SelectionChangedCmd { get; }
        private IDataService _iDataService;
        public ObservableCollection<Item> MyDataList { get; set; }

        public ObservableCollection<Item> DataList
        {
            get { return dataList;}
            set { SetProperty(ref dataList, value); }
        }
        private Item _selectedItem;

        public Item SelectedItem
        {
            get { return _selectedItem; }
            set { SetProperty(ref _selectedItem, value); }
        }
        [ImportingConstructor]
        public RightPaneViewModel(IEventAggregator eventAggregator,IDataService iDataService)
        {
            _iDataService = iDataService;
               _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<Refresh>().Subscribe(OnRefresh);
            _eventAggregator.GetEvent<SelectChange>().Subscribe(OnSelectChange);
            DataList = new ObservableCollection<Item>(this.GetNumbers());
            SelectionChangedCmd = new DelegateCommand<ListBox>(ExecuteSelectionChangedCmd);
        }

        private void ExecuteSelectionChangedCmd(ListBox lst)
        {
            SeleItem = lst.SelectedItem as Item;
            if(SeleItem==null)return;
            _eventAggregator.GetEvent<DownLoad>().Publish(SeleItem);
        }
        private void OnRefresh(long num)
        {
            DataList = new ObservableCollection<Item>(this.GetNumbers());
        }
        private void OnSelectChange(RadTreeViewItem item)
        {
            MyDataList = new ObservableCollection<Item>();
            foreach (var list in DataList)
            {
                var extension = System.IO.Path.GetExtension(list.FileFullName);
                switch (item.Header.ToString())
                {
                    case "全部文件":
                        MyDataList.Add(list);
                        break;
                    case "图片":
                      
                        if (extension != null && extension.ToString() == ".png")
                        {
                            MyDataList.Add(list);
                        }
                        break;
                    case "文档":
                        if (extension != null && extension.ToString() == ".txt")
                        {
                            MyDataList.Add(list);
                        }
                        break;
                    case "视频":
                        break;
                    case "种子":
                        break;
                    case "音乐":
                        break;
                    case "应用":
                        break;
                    case "其他":
                        if (extension != null && extension.ToString() == ".zip")
                        {
                            MyDataList.Add(list);
                        }
                        break;
                    case "我的分享":
                        break;
                }

             
            }
            DataList.Clear();
            DataList = MyDataList;
        }
        public void OnRefresh()
        {
            DataList = new ObservableCollection<Item>(this.GetNumbers());
        }

        public Item SeleItem { get; set; }

        private IEnumerable<Item> GetNumbers()
        {
         
            String path = _iDataService.HostUrl;
            DirectoryInfo folder = new DirectoryInfo(path);

            //遍历文件
            foreach (FileInfo file in folder.GetFiles("*.*"))
            {
                string extension = System.IO.Path.GetExtension(file.FullName);
                if (extension == ".txt")
                {
                    image = new BitmapImage(new Uri("../Assets/txt.png", UriKind.Relative));
                }
                else if (extension == ".png")
                {
                    image = new BitmapImage(new Uri("../Assets/picture.png", UriKind.Relative));
                }
                else if (extension == ".zip")
                {
                    image = new BitmapImage(new Uri("../Assets/zip.png", UriKind.Relative));
                }
                else
                {
                    image = new BitmapImage(new Uri("../Assets/dept.png", UriKind.Relative));
                }
                yield return new Item
                {
                    Image = image,
                    FileName = file.Name,
                    FileFullName = file.FullName
                };
            }
            //遍历文件夹
            foreach (DirectoryInfo file in folder.GetDirectories())
            {
                image = new BitmapImage(new Uri("../Assets/folder.png", UriKind.Relative));
                yield return new Item
                {
                    Image = image,
                    FileName = file.Name,
                    FileFullName = file.FullName
                };
            }
        }
    }
    

}
